package com.study.userserviceprovider.controller;

import com.study.api.Result;
import com.study.clients.IUserAuthFeignClients;
import com.study.exception.ValidException;
import com.study.userserviceprovider.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description：
 * @author：李军荣
 * @date: 2020/12/28 16:40
 */
@RestController
public class UserAuthFeignController implements IUserAuthFeignClients {

    @Override
    public Result<String> validToken(String token){
        if(StringUtils.isEmpty(token)){
            throw new ValidException("token不能为空");
        }
        try {
            Claims claims = JwtUtil.parseToken(token);
            return new Result.Builder().data(claims.get("uid").toString()).ok().bulider();
        }catch (ExpiredJwtException e){
            return new Result.Builder().customize("token已过期").bulider();
        }catch (SignatureException e){
            return new Result.Builder().customize("签名校验失败").bulider();
        }

    }
}
